package com.example.service.impl;

import com.example.exception.BusinessException;
import com.example.mapper.DeptMapper;
import com.example.pojo.Dept;
import com.example.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceimpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Override
    public List<Dept> findAll() {
        return deptMapper.findAll();
    }

    @Override
    public void deleteByID(Integer id) {
        //判断该部门下是否有员工
        int count = deptMapper.countEmpByDeptId(id);
        if (count > 0) {
            throw new BusinessException("对不起，当前部门下有员工，不能直接删除！");
        }

        // 没有员工，可以删除
        deptMapper.deleteByID(id);
    }

    @Override
    public void insert(Dept dept) {
        //补全基础属性
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        //调用插入数据
        deptMapper.insert(dept);
    }

    @Override
    public Dept getByID(Integer id){
        return deptMapper.getByID(id);

    }

    @Override
    public void update(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.update(dept);

    }


}
